import Vue from "vue";
import Router from "vue-router";
import { Dialog } from "mand-mobile";

Vue.use(Router);
const login = () => import("./views/login");
const home = () => import("./views/home");
let router = new Router({
  // mode: "history",
  // base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      redirect: "/login"
    },
    {
      path: "/login",
      name: "login",
      component: login
    },
    {
      path: "/home",
      name: "home",
      component: home,
      meta: {
        requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
      }
    }
  ]
});
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    // 判断该路由是否需要登录权限
    let token = sessionStorage.getItem("btoken");
    if (token !== null) {
      // 获取当前的token是否存在
      next();
    } else {
      Dialog.alert({
        title: "警告",
        content: "请先进行登录",
        confirmText: "确定",
        onConfirm: () =>
          next({
            path: "/"
          })
      });
    }
  } else {
    next();
  }
});
export default router;
